function Promise(execute){
    this.PromiseState='pending'
    this.PromiseResult=null
    this.callbacks=[]
    let self=this

    function resolve(data){
        if(self.PromiseState!=='pending') return
         //设置状态为成功
        self.PromiseState='fulfilled'
        //返回成功数据
        self.PromiseResult=data
         //等待队列回调 成功
        self.callbacks.forEach(result=>{
            result.onResolved(data)
        })
    }

    function reject(data){
        if(self.PromiseState!=='pending') return
        //设置状态为失败
        self.PromiseState='rejected'
        //返回失败数据
        self.PromiseResult=data
        //等待队列回调 失败
        self.callbacks.forEach(result=>{
            result.onRejected(data)
        })
    }
    /*执行传入的 方法 */
    try{
       execute(resolve,reject)
    }catch(e){
        reject(e)
    }
}

Promise.prototype.then=function(onResolved,onRejected){
   return new Promise((resolve,reject)=>{
       //如果成功 将结果传入第一个方法的参数
       if (this.PromiseState === 'fulfilled') {
           onResolved(this.PromiseResult)
       }
       //如果失败 将结果传入第二个方法的参数
       if (this.PromiseState === 'rejected') {
           onRejected(this.PromiseResult)
       }
       //如果等待 存入数组 实现异步时 多次then回调执行
       if (this.PromiseState === 'pending') {
           this.callbacks.push({
               onResolved: onResolved,
               onRejected: onRejected
           })
       }
   })
    
    
}